<?php
namespace jy_alipay;
defined('BY_JYA') or exit('error');
require_once ROOT_PLUGIN.'jy_alipay/alipay/aop/AopClient.php';
require_once ROOT_PLUGIN.'jy_alipay/alipay/aop/AopCertification.php';
require_once ROOT_PLUGIN.'jy_alipay/alipay/aop/request/AlipayTradeQueryRequest.php';
require_once ROOT_PLUGIN.'jy_alipay/alipay/aop/request/AlipayTradeWapPayRequest.php';
require_once ROOT_PLUGIN.'jy_alipay/alipay/aop/request/AlipayTradeAppPayRequest.php';
require_once ROOT_PLUGIN.'jy_alipay/alipay/aop/request/AlipayTradeCreateRequest.php';
require_once ROOT_PLUGIN.'jy_alipay/alipay/aop/request/AlipaySystemOauthTokenRequest.php';
require_once ROOT_PLUGIN.'jy_alipay/alipay/aop/request/AlipayTradePagePayRequest.php';
require_once ROOT_PLUGIN.'jy_alipay/alipay/aop/request/AlipayTradeRefundRequest.php';
require_once ROOT_PLUGIN.'jy_alipay/alipay/aop/request/AlipayFundAuthOrderAppFreezeRequest.php';
require_once ROOT_PLUGIN.'jy_alipay/alipay/aop/request/AlipayFundAuthOperationDetailQueryRequest.php';
require_once ROOT_PLUGIN.'jy_alipay/alipay/aop/request/AlipayTradePayRequest.php';
require_once ROOT_PLUGIN.'jy_alipay/alipay/aop/request/AlipayFundAuthOrderUnfreezeRequest.php';
require_once ROOT_PLUGIN.'jy_alipay/alipay/aop/request/AlipayEcoContractSignflowsCreateRequest.php';
require_once ROOT_PLUGIN.'jy_alipay/alipay/aop/request/AlipayEcoSignflowsUrlQueryRequest.php';
require_once ROOT_PLUGIN.'jy_alipay/alipay/aop/request/AlipayEcoSignflowsDetailQueryRequest.php';
require_once ROOT_PLUGIN.'jy_alipay/alipay/aop/request/AlipayMerchantOrderSyncRequest.php';
require_once ROOT_PLUGIN.'jy_alipay/alipay/aop/request/AlipayFundAuthOperationCancelRequest.php';
class alipay extends \plugin{
	public function __construct($setting=array()){
		parent::__construct('jy_alipay');
		if((!$setting || !is_array($setting) || !$setting['alipay_appid']) && $_SESSION['this']=='custom'){
			$setting=uni_setting('pay');
		}elseif(!$setting['alipay_appid']){
			$setting=core_setting('pay');
		}
		if(!$setting['alipay_appid'] || !$setting['alipay_private_key'] || !$setting['alipay_public_key']){
			exi('支付宝的appid，私钥，公钥都必须填写','error');
		}
		$aop = new \AopClient ();
		$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
		$aop->target_app_id = $setting['target_app_id']?:'2021001101679589';
		$aop->targetAppId='2021001101679589';
		$aop->appId = $setting['alipay_appid'];
		$aop->rsaPrivateKey = $setting['alipay_private_key'];
		$aop->alipayrsaPublicKey=$setting['alipay_public_key']?:'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsjUMEvM/U6Tj2p4ofJFzyx2jdA+o5VYHIRKjozPAw3uCgvaPZEsldcin9pa1doeB4ZdkJ+NFjewUCB53LNieXAmhV1iO8//5O63PmnPrdO1QC82NRUrosUdiMqX5ZVgeG3g96spl31kr46HUr2SnPD2Z+d5tuoMjA6U9yw4fgQAGlcOZMzVnzBS+e56NB81PKh5ViyQIeOt7qXdeaQPlJZFwsAz1E1MbglSXwfaihoYGnTXbz8w+7ZoiThVuae4+UMGQanyQ6qGClsEO+nGGVjrpUN/H0wMxzc0wjKVVlBXbBR6MS6JSEpzm9kmEl2qaq7S8mjfQ62j58qQQ/0IZ4wIDAQAB';
		$aop->apiVersion = '1.0';
		$aop->signType = 'RSA2';
		$aop->postCharset='utf-8';
		$aop->format='json';
		$aop->notify_url=SITEROOT.$_SESSION['this'].'/payresult/alipay_notify.php';
		$this->aop=$aop;
		
	}
	public function request($request,$access_token=null){
		if($access_token){
			$result = $this->aop->execute($request,$access_token); 
		}else{
			$result = $this->aop->execute($request); 
		}
		$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
		$res =(array)$result->$responseNode;
		if(empty($res)){
			exi('错误代码：参数错误(支付宝应用私钥/支付宝公钥设置错了)/未设置白名单！','error');
		}
		if(!empty($res['code']) && $res['code'] != 10000){
			exi("请求支付宝接口出错！错误码：{$res['code']}，错误内容：{$res['msg']}，{$res['sub_msg']}！",'error');
		} else{
			return $res;
		}
	}
	/*
		支付宝小程序支付，返回支付宝交易单号，用于小程序端发起支付
	*/
	public function aliapp_pay($params){
		$request = new \AlipayTradeCreateRequest();
		$request->setNotifyUrl($this->aop->notify_url);
		$content=array(
			'out_trade_no'=>$params['ordersn'],
			//'seller_id'=>$setting['seller_id'],//卖家支付宝用户ID，可选
			'total_amount'=>$params['fee'],
			'subject'=>$params['title']?:'购买商品',//订单标题
			//'body'=>$params['body'],//对交易或商品的描述
		);
		if(substr($_SESSION['openid'],0,4)=='2088'){
			$content['buyer_id']=$params['buyer_id']?:$_SESSION['openid'];
		}else{
			$content['buyer_open_id']=$params['buyer_id']?:$_SESSION['openid'];
		}
		//ISV扫码点餐
		$jy_aliisv = $this->plugin_exists('jy_aliisv');
		if($jy_aliisv){
			$jy_aliisv = plugin('jy_aliisv')->plugin_setting('basic');
			if($jy_aliisv['pid'] && $jy_aliisv['merchant_shop_id']){
				$content['extend_params']=array(
					'food_order_type'=>$params['food_order_type']?:'qr_order'
				);
			}
		}
		$content=ijson_encode($content);
		$request->setBizContent($content);
		$result = $this->request($request);
		return $result['trade_no'];
	}
	/*
		小程序获取用户信息。返回用户信息数组
	*/
	public function aliapp_oauth($code,$RefreshToken=''){
		$request = new \AlipaySystemOauthTokenRequest ();
		$request->setGrantType("authorization_code");
		$request->setCode($code);
		if($RefreshToken){
			$request->setRefreshToken($RefreshToken);
		}
		$result = $this->request($request);
		$result['user_id']=$result['user_id']?:$result['open_id'];		
		if($result['user_id']){
			$_SESSION['openid']=$result['user_id'];
			//$access_token=$result['access_token'];
			//$request = new \AlipayUserInfoShareRequest ();
			//$res = $this->request($request,$access_token);
		}
		return $result;
	}
	//异步通知验签，传入收到的请求参数，一般为 $_POST 。返回 bool值 true 或者 false
	public function check_sign($data){
		return $this->aop->rsaCheckV1($data,$this->aop->alipayrsaPublicKey,'RSA2');
	}
	/*
		手机网页支付，返回html
	*/
	public function mobile_pay($params){
		$request = new \AlipayTradeWapPayRequest();
		$request->setReturnUrl($params['return_url']);
		$request->setNotifyUrl($this->aop->notify_url);
		$content=array(
			'out_trade_no'=>$params['ordersn'],
			'total_amount'=>$params['fee'],
			'subject'=>$params['title']?:'购买商品',//订单标题
			//'body'=>$params['body'],//对交易或商品的描述
			'timeout_express'=>'90m',
			'product_code'=>'QUICK_WAP_WAY',
		);
		$content=ijson_encode($content);
		$request->setBizContent($content);
		$result = $this->aop->pageExecute ($request); 
		return $result;
	}
	/*
		电脑网页支付，返回html
	*/
	public function pc_pay($params){
		$request = new \AlipayTradePagePayRequest ();  
		$request->setReturnUrl($params['return_url']);
		$request->setNotifyUrl($this->aop->notify_url);
		$content=array(
			'out_trade_no'=>$params['ordersn'],
			'total_amount'=>$params['fee'],
			'subject'=>$params['title']?:'购买商品',//订单标题
			//'body'=>$params['body'],//对交易或商品的描述
			'product_code'=>'FAST_INSTANT_TRADE_PAY',
		);
		$content=ijson_encode($content);
		$request->setBizContent($content);  
		$result = $this->aop->pageExecute($request);
		return $result;
	}
	/*
		APP支付，返回app端可用的参数集
	*/
	public function app_pay($params){
		$request = new \AlipayTradeAppPayRequest();
		$content=array(
			'out_trade_no'=>$params['ordersn'],
			'total_amount'=>$params['fee'],
			'subject'=>$params['title']?:'购买商品',//订单标题
			//'body'=>$params['body'],//对交易或商品的描述
			'product_code'=>'QUICK_MSECURITY_PAY',
			'timeout_express'=>'30m',
		);
		$content=ijson_encode($content);
		$request->setBizContent($content);  
		$request->setNotifyUrl($this->aop->notify_url);
		//这里和普通的接口调用不同，使用的是sdkExecute
		$result = $this->aop->sdkExecute($request);
		return $result;
	}
	/*
		统一收单交易退款接口
	*/
	public function refund($params){
		$request = new \AlipayTradeRefundRequest();
		$content=array(
			'out_trade_no'=>'',
			'trade_no'=>$params['transid'],
			'refund_amount'=>$params['refund_fee'],
			'refund_reason'=>$params['refund_desc']?:'购买商品',
		);
		$content=ijson_encode($content);
		$request->setBizContent($content);
		$result = $this->aop->execute($request);
		$result = (array)$result;
		$result = (array)$result['alipay_trade_refund_response'];
		return $result;
	}	
	/*
		$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['aliapp_id'],'uniacid'=>$_SESSION['uniacid']));
		$setting=array(
		'alipay_appid'=>$wxapp['ali_appid'],
		'alipay_private_key'=>$wxapp['alipay_private_key'],
		'alipay_public_key'=>$wxapp['alipay_public_key'],
		);
		$return['trade_no']=model('jy_alipay/alipay',$setting)->aliapp_pay_xinyong($params);
	*/
	//创建免押订单
	public function aliapp_pay_xinyong($params){
		$object['out_order_no'] = $params['ordersn'];
		$object['out_request_no'] = $params['ordersn'];
		$object['order_title'] = $params['title']?:'订单押金';
		$object['amount'] = $params['yafee'];
		$object['product_code'] ='PRE_AUTH_ONLINE';
		$object['notify_url'] = SITEROOT.$_SESSION['this'].'/payresult/alipay_mianya_notify.php';
		$extra_param['category'] = 'RENT_PHONE';
		$extra_param['serviceId'] = $params['ali_service_id'];
		$object['extra_param'] = json_encode($extra_param);
		$json = json_encode($object);
		$request = new \AlipayFundAuthOrderAppFreezeRequest();
		//$request->setNotifyUrl('http://api.test.alipay.net/atinterface/receive_notify.htm');
		$request->setBizContent($json);
		$this->aop->notify_url=$object['notify_url'];
		$result = $this->aop->sdkExecute($request);
		//$result = $result->getBody();
		return $result;
	}
	//取消免押订单
	public function aliapp_cancel_xinyong($params){
		$object['out_order_no'] = $params['ordersn'];
		$object['out_request_no'] = $params['ordersn'];
		$object['remark'] = '授权撤销';
		$json = ijson_encode($object);
		$request = new \AlipayFundAuthOperationCancelRequest();
		//$request->setNotifyUrl('http://api.test.alipay.net/atinterface/receive_notify.htm');
		$request->setBizContent($json);
		$result=$this->request($request);
		return $result;
	}
	public function cha_pay($out_order_no=''){
		$object['out_order_no'] = $out_order_no;
		$object['out_request_no'] = $object['out_order_no'];
		$object['operation_type'] = 'FREEZE';
		$json = json_encode($object);
		$request = new \AlipayFundAuthOperationDetailQueryRequest ();
		$request->setBizContent($json);
		$result = $this->aop->execute($request);
		$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
		$res =(array)$result->$responseNode;
		if($res['code']==10000){
			$paylog=pdo_get('core_paylog',array('tid'=>$object['out_order_no']));
			$fans=pdo_get('core_fans',array('mid'=>$paylog['mid']));
			//解冻
			$object2['out_request_no'] = $object['out_order_no'];//$paylog['tid'];
			$object2['auth_no'] = $res['auth_no'];//$paylog['tid'];
			$object2['amount'] = $res['amount'];
			$object2['remark'] = '解冻资金';
			$json = json_encode($object2);
			$request = new \AlipayFundAuthOrderUnfreezeRequest();
			$request->setBizContent($json);
			$result2 = $this->aop->execute($request);
			$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
			$res2 =(array)$result2->$responseNode;
		}
		return $res2;
	}
	//发起扣款
	public function xinyong_pay($paylog=array()){
		$object['out_trade_no'] = $paylog['tid'];
		$object['total_amount'] = $paylog['fee'];
		$object['subject'] = $paylog['title'];
		$object['product_code'] ='PRE_AUTH_ONLINE';
		$object['auth_confirm_mode'] ='COMPLETE';
		$json = json_encode($object);
		$request = new \AlipayTradePayRequest();
		$request->setNotifyUrl(SITEROOT.$_SESSION['this'].'/payresult/alipay_notify.php');
		$request->setBizContent($json);
		$result=$this->request($request);
		return $result;
	}
	public function xinyong_jiedong($params=array()){
	    $paylog=pdo_get('core_paylog',array('id'=>$params['pay_id'],'uniacid'=>$_SESSION['uniacid']));
		$diyfields=json_decode($paylog['diyfields'],true);
		$object['out_request_no'] = 't'.$params['ordersn'];
		$auth_no=$diyfields['auth_no']?:$params['auth_no'];
		$object['auth_no'] = $auth_no?:'';
		$object['amount'] = $paylog['oldfee'];
		$object['remark'] = '解冻资金';
		$json = json_encode($object);
		$request = new \AlipayFundAuthOrderUnfreezeRequest();
		$request->setBizContent($json);
		$result=$this->request($request);
		return $result;
	}
	//E签宝api文档:https://open.esign.cn/doc/opendoc/apiv3-guide/al7kcc
	//E签宝文档：https://opendocs.alipay.com/open/00pq4d?pathHash=d1c8ace8
	//alipay.eco.contract.signflows.create(创建电子合同签署流程(E签宝)) https://opendocs.alipay.com/apis/00prvn
	public function ehtong_signflows_create($params){
		$jy_aliesign=plugin('jy_aliesign')->plugin_setting('basic');
		if($jy_aliesign['api_type']){
			$res = model('jy_aliesign/api')->ehtong_signflows_create($params);
			$res['isapi']=1;
			return $res;
		}
		$paylog = pdo_get('core_paylog',array('tid'=>$params['ordersn'],'uniacid'=>$_SESSION['uniacid']));
		$fans=pdo_get('core_fans',array('mid'=>$paylog['mid'],'uniacid'=>$_SESSION['uniacid']));
		$openid = $fans['openid'];
		//创建合同
		$template_id =$params['esigntpid'];
		$fill_contents = $this->get_esign_fill_contents($paylog['oid']);
		$realname=pdo_getcolumn('core_members',array('id'=>$paylog['mid'],'uniacid'=>$_SESSION['uniacid']),'realname');
		$signer = [
			'name'=>$realname?:'未知姓名',
			'third_party_user_id'=>$_SESSION['openid']?:$openid,
		];
		$signfields = array(
			array('struct_key'=>'signer1','sign_field_type'=>'person','signer'=>$signer),
		);
		if($jy_aliesign['sign_field_type']){
			$signer = [
				'third_party_user_id'=>$jy_aliesign['third_party_user_id'],
				'org_name'=>$jy_aliesign['org_name'],
				'org_id_type'=>$jy_aliesign['org_id_type'],
				'org_id_number'=>$jy_aliesign['org_id_number'],
				'org_legal_name'=>$jy_aliesign['org_legal_name'],
				'org_legal_id_type'=>$jy_aliesign['org_legal_id_type'],
				'org_legal_id_number'=>$jy_aliesign['org_legal_id_number'],
			];
			$signfields[]=array('struct_key'=>'signer2','sign_field_type'=>'org','signer'=>$signer);
		}else{
			$signfields[]=array('struct_key'=>'signer2','sign_field_type'=>'platform');
		}
		$config_info = [
			'platform_order_no'=>time(),
			'collect_attachement'=>false,
		];
		$request = new \AlipayEcoContractSignflowsCreateRequest();
		$object['business_scene'] = '租赁交付单';
		$object['template_infos'] = array(
			array(
				'template_id'=>$template_id,
				'fill_contents'=>$fill_contents,
				'signfields'=>$signfields,
			)
		);
		$object['config_info'] = json_encode($config_info);
		$json = json_encode($object);
		$request->setBizContent($json);
		$result = $this->aop->execute($request,null,null,'2021001101679589');
		$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
		$res2 =(array)$result->$responseNode;
		if(!$res2['flow_id']){
			return ['code'=>0,'msg'=>$res2['sub_msg']];
		}
		//$ret=$this->ehtong_signdocs($res2['flow_id']);
		if($res2['file_url']){
				//保存合同
				$ext=substr(strrchr($res2['file_url'],'.'),1);
				$docsstr=@file_get_contents($res2['file_url']);
				$file_hash=hash("sha256", $docsstr);
				$file_url=cfc('files')->source_save($docsstr,'other',$ext);
		}
		$flow_id = $res2['flow_id'];
		return array('code'=>1,'flow_id'=>$flow_id,'file_name'=>$res2['file_name'],'file_url'=>$file_url,'file_hash'=>$file_hash);
	}
	//alipay.eco.signflows.url.query(获取签署地址)
	public function ehtong_signflows_url($params){
		$request = new \AlipayEcoSignflowsUrlQueryRequest();
		$object['flow_id'] = $params['flow_id'];
		$object['third_party_user_id'] = $params['mid'];
		$json = json_encode($object);
		$request->setBizContent($json);
		$result=$this->aop->execute($request,null,null,'2021001101679589'); 
		$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
		$res2 =(array)$result->$responseNode;
		if($res2['code']!=10000){
			return ['code'=>0,'msg'=>$res2['sub_msg']];
		}
		return $res2;
	}
	//获取签约文档下载地址；
	public function ehtong_signdocs($flow_id='',$isexi=0){
		$request = new \AlipayEcoSignflowsDetailQueryRequest();
		$object=array(
			"flow_id"=>$flow_id
		);
		$json = ijson_encode($object);
		$request->setBizContent($json);
		$result=$this->aop->execute($request,null,null,'2021001101679589'); 
		$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
		$res = (array)$result->$responseNode;
		if($res['code'] && $res['code']!=10000){
			if($isexi){
				exi('获取签约合同地址失败！'.$res['code'].','.$res['msg'],'error');
			}else{
				return array('code'=>$res['code'],'msg'=>$res['sub_msg']);
			}
		}
		$docs=json_decode(json_encode($res['docs']),true);
		return $docs;
	}
	public function get_esign_fill_contents($order_id){
		$order=pdo_get('jy_weishop_order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		$api_data=json_decode($order['api_data'],true);
		$allqi = $api_data['allqi'];
		$order['api_data'] = $api_data;
		$address=pdo_get('core_address',array('id'=>$order['addressid'],'uniacid'=>$_SESSION['uniacid']));
		$pcd = json_decode($address['pcd'],true);
		$addressinfo =$pcd['province'].$pcd['city'].$pcd['district'].$address['address'];
		$cardnum=pdo_getcolumn('core_members',array('id'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']),'cardnum');
		$realname=pdo_getcolumn('core_members',array('id'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']),'realname');
		if(!$cardnum || !$realname){
		   	exi('请先实名认证！身份证号码为空','error'); 
		}
		$data[] = array('struct_key'=>'name','value'=>$realname,'structId'=>'name');
		$data[] = array('struct_key'=>'code','value'=>$cardnum,'structId'=>'code');
		$data[] = array('struct_key'=>'mobile','value'=>$address['mobile'],'structId'=>'mobile');
		$data[] = array('struct_key'=>'address','value'=>$addressinfo,'structId'=>'address');
		$data[] = array('struct_key'=>'email','value'=>'','structId'=>'email');
		$data[] = array('struct_key'=>'order_sn','value'=>$order['ordersn'],'structId'=>'order_sn');
		//$data[] = array('struct_key'=>'order_sn2','value'=>$order['ordersn'],'structId'=>'order_sn');
		$order_goods = pdo_get('jy_weishop_order_goods',array('orderid'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
		$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$order_goods['optionid'],'uniacid'=>$_SESSION['uniacid']),'specs');
		$specs=explode("_",$specs);
		foreach($specs as &$spec){
			$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec,'uniacid'=>$_SESSION['uniacid']),'title');
		}
		$goods_optiontitle=implode("，",$specs);
		$goods = pdo_get('jy_weishop_goods',array('id'=>$order_goods['goodsid'],'uniacid'=>$_SESSION['uniacid']));	
		$goods_brand=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$goods['pcate'],'uniacid'=>$_SESSION['uniacid']),'title');
		$data[] = array('struct_key'=>'goods_name','value'=>$goods['title'],'structId'=>'goods_name');
		$data[] = array('struct_key'=>'taocan_name','value'=>$goods_optiontitle,'structId'=>'taocan_name');
		$data[] = array('struct_key'=>'goods_desc','value'=>$goods['title'],'structId'=>'goods_desc');
		$data[] = array('struct_key'=>'goods_brand','value'=>$goods_brand?:'无','structId'=>'goods_brand');
		$data[] = array('struct_key'=>'goods_optiontitle','value'=>$goods_optiontitle,'structId'=>'goods_optiontitle');
		$data[] = array('struct_key'=>'goods_total','value'=>$order_goods['total'],'structId'=>'goods_total');
		$data[] = array('struct_key'=>'goods_totalzu','value'=>$api_data['allprice'],'structId'=>'goods_totalzu');
		//$data[] = array('struct_key'=>'goods_danzu','value'=>$api_data['price'],'structId'=>'goods_danzu');
		$data[] = array('struct_key'=>'goods_price','value'=>$order['goodsprice'],'structId'=>'goods_price');
		//$data[] = array('struct_key'=>'goods_duan','value'=>$order['goodsprice'],'structId'=>'goods_duan');
		$data[] = array('struct_key'=>'zuqi','value'=>$api_data['allqi'],'structId'=>'zuqi');
		//$data[] = array('struct_key'=>'jia_mobile','value'=>$_SESSION['account']['phone'],'structId'=>'jia_mobile');
		//$data[] = array('struct_key'=>'jia_name','value'=>$_SESSION['account']['cname'],'structId'=>'jia_name');
		//$data[] = array('struct_key'=>'jia2_name','value'=>$_SESSION['account']['cname'],'structId'=>'jia2_name');
		//$data[] = array('struct_key'=>'jia_address','value'=>$_SESSION['account']['address'],'structId'=>'jia_address');
		//$data[] = array('struct_key'=>'fangshi','value'=>'分期租赁','structId'=>'fangshi');
		$data[] = array('struct_key'=>'jsyafee','value'=>'0','structId'=>'jsyafee');
		//合同编号
		$jy_aliesign=plugin('jy_aliesign')->plugin_setting('basic');
		$nonce = random(2,true);
		$pre=$jy_aliesign['hetongper']?:'HT';
		$hetongsn=$pre.date('YmdHis').$nonce;
		$data[] = array('struct_key'=>'hetongsn','value'=>$hetongsn,'structId'=>'hetongsn');
		$basic=$this->plugin_setting('basic');
		$qinum=$basic['qinum']?:12;
		$starttime=strtotime($order['api_data']['starttime']);
		for($i=1;$i<=$qinum;$i++){
			$data[]=array('struct_key'=>'zqkey'.$i,'value'=>'第'.$i.'期','structId'=>'zqkey'.$i);
			$day=$starttime+30*($i-1)*24*3600;
			$day=date('Y-m-d',$day);
			$data[]=array('struct_key'=>'zjkey'.$i,'value'=>$order['api_data']['total_price']?:'无','structId'=>'zjkey'.$i);
			$data[]=array('struct_key'=>'zjfkey'.$i,'value'=>$day,'structId'=>'zjfkey'.$i);
			$data[]=array('struct_key'=>'mdkey'.$i,'value'=>$order['api_data']['duan_price']?:'无','structId'=>'mdkey'.$i);
		}
		return $data;
	}
	//alipay.merchant.order.sync(订单数据同步接口)
	public function back_order($order,$isexi=false){
		$jy_aliisv = $this->plugin_exists('jy_aliisv');
		if($jy_aliisv){
			$jy_aliisv = plugin('jy_aliisv')->plugin_setting('basic');
			if(!$jy_aliisv['pid']){
				$maindata=pdo_getcolumn('core_account_plugin_setting',array('uniacid'=>1,'plugin'=>'jy_aliisv','key'=>'basic'),'data');
				$maindata=json_decode($maindata,true);
				$jy_aliisv['pid']=$maindata['pid'];
			}
		}
		if(!$jy_aliisv['pid'] || !$jy_aliisv['merchant_shop_id']){
			if($isexi){
				exi('请输入《侧门店shopid》和联系管理员完善《服务商的ID》！','error');
			}else{
				return false;	
			}
		}
		$request = new \AlipayMerchantOrderSyncRequest();
		$ext_info[]=array(
			'ext_key'=>'merchant_order_status',
			'ext_value'=>'PAID',
		);
		$ext_info[]=array(
			'ext_key'=>'merchant_biz_type',
			'ext_value'=>'QR_FOOD_ORDER',
		);
		$ext_info[]=array(
			'ext_key'=>'merchant_order_link_page',
			'ext_value'=>'/jy_weishop/order/detail/index?id='.$order['id'],
		);
		$ext_info[]=array(
			'ext_key'=>'tiny_app_id',
			'ext_value'=>$this->aop->appId,
		);
		$ext_info[]=array(
			'ext_key'=>'business_info',
			'ext_value'=>array('rebate_pid'=>$jy_aliisv['pid']),
		);
		$shop_info=array(
			'merchant_shop_id'=>$jy_aliisv['merchant_shop_id'],
		);
		$item_order_list=array();
		foreach($order['goods'] as $ov){
			$item_order_list[]=array(
				'item_id'=>$ov['goodsid'],
				'item_name'=>$ov['title'],
				'unit_price'=>$ov['price'],
				'quantity'=>$ov['total'],
			);
		}
		$content=array(
			'out_biz_no'=>$order['pay_id'],
			'record_id'=>$order['ordersn'],
			'buyer_id'=>$order['openid']?:$_SESSION['openid'],
			'amount'=>$order['price'],
			'pay_amount'=>$order['price'],
			'subject'=>$order['title']?:'购买商品',
			'order_type'=>'SERVICE_ORDER',
			'trade_no'=>$order['transid'],
			'order_create_time'=>date('Y-m-d H:i:s',$order['createtime']),
			'order_modified_time'=>date('Y-m-d H:i:s',$order['createtime']).'.123',
			'item_order_list'=>$item_order_list,
			'ext_info'=>$ext_info,
			'shop_info'=>$shop_info,
		);
		if(!$content['buyer_id']){
			$content['buyer_id']=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid'],'wxgid'=>$order['wxapp_id']),'openid');
		}
		$json = json_encode($content);
		//print_r('~~~~'.$json);exit;
		$request->setBizContent($json);
		$result=$this->aop->execute($request); 
		$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
		$res2 =(array)$result->$responseNode;
		
		if($res2['code']!=10000){
			if($isexi){
				exi('同步订单数据失败：'.$res2['code'].$res2['sub_msg'],'error');
			}else{
				return ['code'=>0,'msg'=>$res2['sub_msg']];
			}
		}
		return $res2;
	}
}